﻿using BLL;
using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class SelectBlock : BaseUI.BasePage
    {
        private Lc_BlockBLL blockBLL = new Lc_BlockBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindPage()
        {
        }

        protected void BindData()
        {
            var where = ViewState["Where"] == null ? null : ((string)ViewState["Where"]);
            if (FORM_CHECK_VALUE.Value != "")
            {
                if (where == null)
                {
                    where = string.Format("N_BLOCK_ID NOT IN ({0})", FORM_CHECK_VALUE.Value);
                }
                else
                {
                    where += string.Format(" AND N_BLOCK_ID NOT IN ({0})", FORM_CHECK_VALUE.Value);
                }
            }
            var orderby = ViewState["Orderby"] == null ? null : ((string)ViewState["Orderby"]);
            int count;
            var dt = blockBLL.GetDataByPage(0, 1000, where, orderby, out count);
            DATA_TABLE.DataSource = dt;
            DATA_TABLE.DataBind();
        }

        protected void SearchClick(object sender, EventArgs e)
        {
            string where = null;
            if (FORM_SEARCH_VALUE.Value != "")
            {
                where = "C_BLOCK_NAME LIKE '%" + FORM_SEARCH_VALUE.Value + "%'";
                if (IsInt(FORM_SEARCH_VALUE.Value))
                {
                    where = "( " + where + " OR N_BLOCK_ID = " + FORM_SEARCH_VALUE.Value + " )";
                }
            }
            ViewState["Where"] = where;
            BindData();
        }

        protected void CheckClick(object sender, EventArgs e)
        {
            var text = FORM_CHECK_VALUE_STRING.Text;
            var value = FORM_CHECK_VALUE.Value;
            var count = 0;
            foreach (GridViewRow item in DATA_TABLE.Rows)
            {
                var check = (CheckBox)item.FindControl("chkSelectId");
                if (check.Checked)
                {
                    var _text = item.Cells[2].Text;
                    if (text != "")
                    {
                        text += "," + _text;
                        value += "," + check.ToolTip;
                    }
                    else
                    {
                        text += _text;
                        value += check.ToolTip;
                    }
                    count++;
                }
            }
            if (count == 0)
            {
                MessageBox.Show(this, "至少选择一个！");
            }
            else
            {
                FORM_CHECK_VALUE_STRING.Text = text;
                FORM_CHECK_VALUE.Value = value;
                BindData();
            }
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            ViewState["Orderby"] = SortOrder + " " + OrderDire;
            BindData();
        }

        public bool IsInt(string value)
        {
            return Regex.IsMatch(value, @"^\d*$");
        }
    }
}